<template>
  <div>
    <el-card>
      <el-row :gutter="15">
        <el-col :span="4">
          <el-card class="leftBox" shadow="never" :body-style="{padding: '0px'}">
            <el-collapse v-model="activeName" accordion>
              <el-collapse-item title="按类型查看" name="1">
                <div class="selectArea">
                  <el-tree
                    ref="treeone"
                    :data="optionsType"
                    :expand-on-click-node="false"
                    node-key="mat_type"
                    :default-expanded-keys="[0]"
                    :props="{ children: 'children', label: 'mat_type_name', type: 'mat_type' }"
                    @node-click="handleTypeClick">
                  </el-tree>
                </div>
              </el-collapse-item>
            </el-collapse>
          </el-card>
        </el-col>
        <el-col :span="20">
            <div class="clearfix" v-if="crud.props.searchToggle">

              <!-- 搜索 -->
              <el-input v-model="query.name" clearable placeholder="输入节目名称" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
              <rrOperation />
              <el-radio v-model="query.flag" label="all" @change="crud.toQuery()" style="margin-left: 25px;">全部节目</el-radio>
              <el-radio v-model="query.flag" label="0" @change="crud.toQuery()">有效的节目</el-radio>
              <el-radio v-model="query.flag" label="1" @change="crud.toQuery()">失效的节目</el-radio>
              <!-- <el-checkbox true-label="1" false-label="0" v-model="query.flag" @change="crud.toQuery()" ></el-checkbox> -->
            </div>
            <crudOperation :permission="permission" style="margin-top: 7px;padding-bottom: 0;" />
            <!--表单组件-->
            <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="550px">
              <el-form ref="form" inline :model="form" :rules="rules" label-width="120px" :inline-message="true" >
                <el-row :gutter="15">
                  <el-col :span="24">
                    <el-form-item label="节目代码" prop="pgm_id">
                      <el-input v-model="form.pgm_id" class="elInputS"  disabled />
                    </el-form-item>
                    <el-form-item label="节目名称" prop="pgm_name">
                      <el-input v-model="form.pgm_name" class="elInputS"  />
                    </el-form-item>
                    <el-form-item label="节目类型" prop="pgm_type">
                      <el-cascader
                        v-model="form.pgm_type"
                        :options="optionsType[0].children"
                        :props="{emitPath: false, value: 'mat_type_id', label: 'mat_type_name'}"
                        placeholder="请选择节目类型"
                        :show-all-levels="false"
                        style="margin-left: 0;"
                        class="elInputS">
                      </el-cascader>
                    </el-form-item>
                    <el-form-item label="素材" prop="mat_id">
                      <el-input v-model="form.matinfo" class="elInputS" readonly />
                      <el-button icon="el-icon-plus" @click="innerdialogVisible = true"></el-button>
                    </el-form-item>
                    <el-form-item label="广告系统素材ID" prop="adv_pgm_id">
                      <el-input v-model="form.adv_pgm_id" class="elInputS" />
                    </el-form-item>
                    <el-form-item label="节目长度" prop="pgm_len">
                      <time-picker v-model="form.pgm_len" class="elInputS" />
                    </el-form-item>
                    <el-form-item label="入点" prop="markin">
                      <time-picker v-model="form.markin" class="elInputS" disabled />
                    </el-form-item>
                    <el-form-item label="出点" prop="markout">
                      <time-picker v-model="form.markout" class="elInputS" disabled />
                    </el-form-item>
                    <el-form-item label="开播日期" prop="s_play_date">
                      <el-date-picker
                        class="elInputS"
                        value-format="yyyy-MM-dd"
                        style="width: 100%;"
                        v-model="form.s_play_date"
                        type="date"
                        placeholder="选择日期">
                      </el-date-picker>
                    </el-form-item>
                    <el-form-item label="停播日期" prop="e_play_date">
                      <el-date-picker
                        class="elInputS"
                        value-format="yyyy-MM-dd"
                        style="width: 100%;"
                        v-model="form.e_play_date"
                        type="date"
                        placeholder="选择日期">
                      </el-date-picker>
                    </el-form-item>
                    <el-form-item label="创建人" prop="creator">
                      <el-input v-model="form.truename" disabled class="elInputS" />
                    </el-form-item>
                    <el-form-item label="创建日期" prop="create_time">
                      <el-date-picker
                        disabled
                        class="elInputS"
                        value-format="yyyy-MM-dd"
                        style="width: 100%;"
                        v-model="form.create_time"
                        type="date"
                        placeholder="选择日期">
                      </el-date-picker>
                    </el-form-item>
                    <el-form-item label=" ">
                        <el-radio v-model="form.cancel_flag" :label="0">有效</el-radio>
                        <el-radio v-model="form.cancel_flag" :label="1">失效</el-radio>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                      <el-input v-model="form.remark" class="elInputM"  />
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button type="text" @click="crud.cancelCU">取消</el-button>
                <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
              </div>
              <!-- 封装的素材弹窗组件 prop:
                :innerVisible  // 控制弹窗隐藏显示
                v-model        // 接收返回的素材对象
                @change        // 选中素材改变点击确定后的回调事件.
                @close         // 弹窗点击关闭的回调事件，用于告知父组件，由父控制关闭或显示
              -->
              <mat-dialog
                :innerVisible="innerdialogVisible"
                v-model="matSelectRows"
                mattype="all"
                :multify="false"
                @dragchange="matSelectChange"
                @close="listenClose">
              </mat-dialog>
            </el-dialog>
            <!-- 列表区域 -->
            <el-table
              height="70vh"
              ref="table"
              highlight-current-row
              @current-change="rowsCurrentChange"
              v-loading="crud.loading"
              lazy
              :data="crud.data"
              border
              stripe
              @select="crud.selectChange"
              @select-all="crud.selectAllChange"
              @selection-change="crud.selectionChangeHandler"
              @sort-change="handleSortEvent"
              style="width: 100%;">
              <el-table-column :selectable="checkboxT" type="selection" width="55" />
              <el-table-column prop="pgm_id" label="代码" width="80px" sortable="custom"></el-table-column>
              <el-table-column prop="pgm_name" label="节目名称" sortable="custom" show-overflow-tooltip></el-table-column>
              <el-table-column prop="pgm_type" label="节目类型" sortable="custom" width="120px" show-overflow-tooltip>
                <template v-slot="scope">
                  {{scope.row.pgm_type_name}}
                </template>
              </el-table-column>
              <el-table-column prop="mat_id" label="素材ID" width="90px" sortable="custom"></el-table-column>
              <el-table-column prop="markin" label="入点" sortable="custom" width="110px">
                <template v-slot="scope">
                  <time-picker inputType="readonly" format="HH:ii:ss" v-model="scope.row.markin" />
                </template>
              </el-table-column>
              <el-table-column prop="markout" label="出点" sortable="custom" width="110px">
                <template v-slot="scope">
                  <time-picker inputType="readonly" format="HH:ii:ss" v-model="scope.row.markout" />
                </template>
              </el-table-column>
              <el-table-column prop="pgm_len" label="节目长度" width="110px" sortable="custom">
                <template v-slot="scope">
                  <time-picker inputType="readonly" format="HH:ii:ss" v-model="scope.row.pgm_len" />
                </template>
              </el-table-column>
              <el-table-column prop="creator" label="创建人" width="90px">
                <template v-slot="scope">
                  {{scope.row.truename}}
                </template>
              </el-table-column>
              <el-table-column prop="create_time" label="创建日期" sortable="custom" width="120px">
                <template v-slot="scope">
                  {{scope.row.create_time | subTimeDate}}
                </template>
              </el-table-column>
              <el-table-column prop="adv_pgm_id" label="广告系统素材ID" width="120px"></el-table-column>
              <el-table-column label="操作" fixed="right" width="120">
                <template slot-scope="scope">
                  <udOperation
                    :data="scope.row"
                    :permission="permission"
                    :disabled-dle="scope.row.id === 1"
                    msg="确定删除吗,此操作不能撤销！"
                  />
                </template>
              </el-table-column>
            </el-table>
            <!--分页组件-->
            <pagination />
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@/components/Crud/crud'
import crudAdvlib from '@/api/pgmmanage/advlib.js' // 引入当前栏目的后端api
import { matTlist } from '@/api/material/mattype.js'
import commonMethods from '@/utils/commonMethods.js'
import { dateFormat } from '@/utils/commonFun.js'
import { pgmInfoConfig } from '@/sysconfig.js' // 引入节目库系统配置
import { channelMenuList } from '@/api/channel/channel.js' // 频道列表接口

// 引入已封装好的增删改查相关组件
import rrOperation from '@/components/Crud/RrOperation.vue' // 搜索查询组件
import crudOperation from '@/components/Crud/CrudOperation.vue' // 列表顶部功能区域组件
import pagination from '@/components/Crud/Pagination.vue' // 分页组件
import udOperation from '@/components/Crud/UD.operation' // 列表操作列组件
import MatDialog from '@/components/MatdialogComponents.vue' // 素材列表弹窗组件

// 配置表单字段， 并设置字段初始值
const defaultForm = {
  pgm_id: 0,
  pgm_name: null,
  pgm_type: null,
  mat_id: null,
  markin: 0,
  markout: 0,
  pgm_len: 0,
  s_play_date: dateFormat(new Date(), 'ymd'),
  e_play_date: dateFormat(new Date(), 'ymd'),
  creator: null,
  create_time: dateFormat(new Date(), 'ymd'),
  remark: null,
  adv_pgm_id: null,
  cancel_flag: 0
}
export default {
  name: 'advlib',
  components: { rrOperation, crudOperation, pagination, udOperation, MatDialog },
  cruds() { // 该方法，返回当前组件配置初始化后的实例对象
    return CRUD({ idField: 'pgm_id', title: '节目库管理', url: 'pgmmanage/advlib', crudMethod: { ...crudAdvlib }, sort: ['pgm_id,desc'] })
  },
  mixins: [presenter(), header(), form(defaultForm), crud(), commonMethods],
  provide() {
    return {
      editroom: this
    }
  },
  filters: {
    subTimeDate: function(value) {
      if (value !== null && value.length > 10) {
        return value.substr(0, 10)
      }
      return value
    }
  },
  data() {
    // 验证邮箱规则
    var checkPgmLen = (rule, value, callback) => {
      if (value > 0) {
        return callback()
      } else {
        return callback(new Error('请输入节目长度!'))
      }
    }

    return {
      /* 设置页面权限 */
      permission: {
        add: ['admin', 'advlib::add'],
        edit: ['admin', 'advlib::edit'],
        del: ['admin', 'advlib::del']
      },
      rules: {
        pgm_name: [
          { required: true, message: '节目名称不能为空', trigger: 'blur' }
        ],
        pgm_type: [
          { required: true, message: '节目类型不能为空', trigger: 'blur' }
        ],
        pgm_len: [
          { validator: checkPgmLen, trigger: 'blur' } // 检查节目长度
        ]
      }, // 表单验证规则
      activeName: '1',
      optionsType: [
        {
          mat_type_name: '广告',
          mat_type: 0,
          children: []
        }
      ],
      pgmInfoConfig, // 节目库系统配置
      // 设备型号配置数组
      deviceFlag: [
        { id: '1', label: 'linux服务器' }
      ],
      channelList: [], // 频道列表
      rowscurrent: null,
      innerdialogVisible: false,
      matSelectRows: null
    }
  },
  async created() {
    // console.log(this.pgmInfoConfig)
    this.query.flag = 'all'
    this.query.type = []
    this.crud.optShow.download = false // 隐藏 导出按钮
    // 获取节目类型菜单
    this.getmatTypeList()
    this.channelList = await channelMenuList()
    console.log(this.channelList)
  },
  mounted() {

  },
  methods: {
    getmatTypeList() {
      matTlist({ id: 34 }).then(res => {
        if (res.status !== 200) {
          console.log('获取失败')
        }
        this.optionsType[0].children = res.data.data
      })
    },
    checkboxT(row, rowIndex) {
      return row.id !== false
    },
    // 按广告类型查看
    handleTypeClick(data, node, el) {
      if (this.$refs.treeone.currentNode !== null) {
        this.$refs.treeone.currentNode.$el.setAttribute('class', 'el-tree-node')
      }
      el.$el.setAttribute('class', 'el-tree-node is-current is-focusable') // 设置选中
      if (data.mat_type_id === undefined) {
        this.query.type = 'all'
      } else {
        this.query.type = this.findNodeChildValArr(data, 'mat_type_id', []).join(',')
      }
      this.crud.toQuery()
    },
    // 排序点击
    handleSortEvent(sort) {
      const sortArr = { descending: 'desc', ascending: 'asc' }
      if (sort.order !== null) {
        this.crud.sort = sort.prop + ',' + sortArr[sort.order]
      } else {
        this.crud.sort = ''
      }
      this.crud.toQuery()
    },
    // 单元行选中事件
    rowsCurrentChange(rows) {
      this.rowscurrent = rows
    },
    // 新增编辑前回调方法
    [CRUD.HOOK.beforeToCU](crud, form) {
      if (typeof form.pgm_type_name !== 'undefined') {
        delete form.pgm_type_name
      }
      if (form.mat_id === null && typeof form.matinfo !== 'undefined') {
        delete form.matinfo
      }
      console.log(form)
    },
    listenClose(state) {
      this.innerdialogVisible = state
    },
    // 素材列表弹窗单元行选中返回事件
    matSelectChange(val) {
      console.log(val)
      if (val !== null) {
        this.form.markin = val.markin
        this.form.markout = val.markout
        this.form.pgm_len = val.file_len
        this.form.mat_id = val.mat_id
        this.form.matinfo = val.mat_name + '[' + val.mat_id + ']'
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .el-form-item {
    margin-bottom: 3px !important;
  }
  .operator {
    float: right;
    display: inline-block;
    margin-left: 20px;
  }
  .detailOpera .el-button {
    margin-top: 15px;
  }
  .leftBox {
    height: 81vh;
    background-color: rgba(222, 223, 230, 0.6);
  }
  .footCard {
    border:  1px solid rgba(222, 223, 230, 1);
    margin-top: 10px;
  }
</style>
